package com.elm.user.controller;


import com.elm.common.pojo.Result;
import com.elm.user.pojo.User;
import com.elm.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping ("/getByIdByPass")
    public Result<User> getUserByIdByPass(@RequestBody User user){
        User u=userService.getUserByIdByPass(user);
        if(u!=null){
            return Result.success(u);
        }
        return Result.error("不存在该用户");
    }

    @GetMapping("/getById")
    public Result<Integer> getUserById(String userId){
        List<User> list=userService.getUserById(userId);
        int rows = list.size();
        return Result.success(rows);
    }

    @PutMapping("/update")
    public Result<Integer> updateUser(@RequestBody User user){
        int rows=userService.updateUser(user);
        if(rows!=0){
            return Result.success(rows);
        }
        return Result.error("添加失败");
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        List<User> list=userService.getUserById(user.getUserId());
        if(list.size()==0){
            userService.register(user);
            return Result.success();
        }
        return Result.error("用户名已存在");
    }

    @PostMapping("/login")
    public Result<String> login(String userId,String password){
        User u=new User();
        u.setUserId(userId);
        u.setPassword(password);
        User loginUser=userService.getUserByIdByPass(u);
        if(loginUser==null){
            return Result.error("账号或密码错误");
        }
        return Result.success("登录成功");
    }

    @GetMapping("/getInfoById")
    public Result<User> getInfo(String userId){
        User u= userService.getInfo(userId);
        return Result.success(u);
    }
}
